package com.chenfubing.ps.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chenfubing.ps.entity.Order;
import com.chenfubing.ps.entity.User;
import com.chenfubing.ps.mapper.OrderMapper;
import com.chenfubing.ps.service.IOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.util.CellRangeAddress;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author chenfubing
 * @since 2020-05-05
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements IOrderService {

    @Resource
    private HttpServletResponse response;

    @Override
    public Page<Order> getOrderPage(int pageNum, int pageSize) {

        return baseMapper.getOrderPage(new Page(pageNum,pageSize));

    }

    @Override
    public void export() {
        List<Order> orderList = baseMapper.selectList(new QueryWrapper<>());
        try {
            String fileName = "车辆信息表";

            response.setCharacterEncoding("UTF-8");
            response.setHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes("UTF-8"), "ISO-8859-1") + ".xls");
            HSSFWorkbook workbook = new HSSFWorkbook();
            // 设置字体
            HSSFFont font = workbook.createFont();
            // 设置字体大小
            font.setFontHeightInPoints((short) 11);
            // 字体加粗
            font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
            // 设置字体名字
            font.setFontName("Courier New");
            // 设置样式;
            HSSFCellStyle style = workbook.createCellStyle();
            // 设置底边框;
            style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
            // 设置底边框颜色;
            style.setBottomBorderColor(HSSFColor.BLACK.index);
            // 设置左边框;
            style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
            // 设置左边框颜色;
            style.setLeftBorderColor(HSSFColor.BLACK.index);
            // 设置右边框;
            style.setBorderRight(HSSFCellStyle.BORDER_THIN);
            // 设置右边框颜色;
            style.setRightBorderColor(HSSFColor.BLACK.index);
            // 设置顶边框;
            style.setBorderTop(HSSFCellStyle.BORDER_THIN);
            // 设置顶边框颜色;
            style.setTopBorderColor(HSSFColor.BLACK.index);
            // 在样式用应用设置的字体;
            style.setFont(font);
            // 设置自动换行;
            style.setWrapText(false);
            // 设置水平对齐的样式为居中对齐;
            style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
            // 设置垂直对齐的样式为居中对齐;
            style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
            HSSFSheet sheet = workbook.createSheet("门禁表");
            sheet.setColumnWidth(0, 110 * 70);
            sheet.setColumnWidth(1, 110 * 70);
            sheet.setColumnWidth(2, 110 * 70);
            sheet.setColumnWidth(3, 110 * 70);
            sheet.setColumnWidth(4, 110 * 70);

            sheet.setDefaultColumnStyle(0,style);
            sheet.setDefaultColumnStyle(1,style);
            sheet.setDefaultColumnStyle(2,style);
            sheet.setDefaultColumnStyle(3,style);
            sheet.setDefaultColumnStyle(4,style);
            HSSFRow titleRow = sheet.createRow(0);
            sheet.addMergedRegion(new CellRangeAddress(0,0,0,4));
            titleRow.createCell(0).setCellValue(fileName);
            HSSFRow row = sheet.createRow(1);
            row.createCell(0).setCellValue("车牌号");
            row.createCell(1).setCellValue("创建时间");
            row.createCell(2).setCellValue("订单状态");
            row.createCell(3).setCellValue("名字");
            int i = 2;
            for (Order access : orderList)
            {
                HSSFRow rows = sheet.createRow(i);
                rows.createCell(0).setCellValue(access.getCarLicense());
                rows.createCell(1).setCellValue(access.getOrderCreatetime().toString());
                rows.createCell(2).setCellValue(access.getOrderState());
                rows.createCell(3).setCellValue(access.getUserName());
                i++;
            }
            workbook.write(response.getOutputStream());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
